package com.rongshu.widget;


import android.app.Dialog;
import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.rongshu.R;

/**
 * created by zhengeweis on 2018/11/2.
 * description：支持输入的dialog
 */

/*
BanyanInputDialog.Builder builder = new BanyanInputDialog.Builder(this);
builder.setTitle("请输入文集名称");
builder.setLeft("取消");
builder.setRight("确定");
builder.setDialogOnClick(new BanyanInputDialog.Builder.onClickListener() {
        @Override
        public void leftClickListener(Object object) {
            }
            @Override
            public void rightClickListener(Object object) {
                  toast(object.toString());
       }
});
BanyanInputDialog banyanInputDialog = builder.create();
banyanInputDialog.show();
 */

public class BanyanInputDialog extends Dialog {
    public BanyanInputDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        TextView titleTv;
        EditText contentTv;
        TextView leftTv;
        TextView rightTv;
        private Context context; // 上下文对象
        private onClickListener listener;
        private int mMainLayout = R.layout.dialog_input;
        private String title;
        private String left;
        private String right;

        @ColorInt
        private int left_color;
        @ColorInt
        private int right_color;

        public interface onClickListener {
            void leftClickListener(Object object);

            void rightClickListener(Object object);
        }

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setDialogOnClick(onClickListener listener) {
            this.listener = listener;
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setLeft(String left) {
            this.left = left;
            return this;
        }

        public Builder setRight(String right) {
            this.right = right;
            return this;
        }

        public Builder setLeftColor(@ColorInt int color) {
            this.left_color = color;
            return this;
        }

        public Builder setRightColor(@ColorInt int color) {
            this.right_color = color;
            return this;
        }

        public BanyanInputDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final BanyanInputDialog dialog = new BanyanInputDialog(context, R.style.CustomDialogStyle);
            final View layout = inflater.inflate(mMainLayout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            titleTv = layout.findViewById(R.id.title);
            contentTv = layout.findViewById(R.id.content);
            leftTv = layout.findViewById(R.id.left);
            rightTv = layout.findViewById(R.id.right);
            titleTv.setText(title);
            leftTv.setText(left);
            rightTv.setText(right);
            leftTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null)
                        listener.leftClickListener(contentTv.getText().toString());
                }
            });
            rightTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null)
                        listener.rightClickListener(contentTv.getText().toString());
                }
            });
            dialog.setContentView(layout);
            return dialog;
        }

    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams params = this.getWindow().getAttributes();
        params.width = (int) getContext().getResources().getDimension(R.dimen.dd_dimen_570px);
        this.getWindow().setAttributes(params);
    }
}
